home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / sharewar / slunec / app / httrack.exe / {app} / src_win / WinHTTrack / htsnostatic.c < prev    next >
C/C++ Source or Header  |  2002-07-10  |  6KB  |  261 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsnostatic.c subroutines:                             */
  34. /*       thread-safe routines for reentrancy                    */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include "htsnostatic.h"
  39.  
  40. #include "htsbase.h"
  41. #include "htshash.h"
  42.  
  43. typedef struct {
  44.   /*
  45.   inthash values;
  46.   */
  47.   inthash blocks;
  48. } hts_varhash;
  49.  
  50. #if USE_BEGINTHREAD
  51. static PTHREAD_LOCK_TYPE hts_static_Mutex;
  52. #endif
  53. static int hts_static_Mutex_init=0;
  54. #if HTS_WIN
  55. #else
  56. static PTHREAD_KEY_TYPE hts_static_key;
  57. #endif
  58.  
  59. int hts_initvar() {
  60.   if (!hts_static_Mutex_init) {
  61.     /* Init done */
  62.     hts_static_Mutex_init=1;
  63. #if USE_BEGINTHREAD
  64.     /* Init mutex */
  65.     htsSetLock(&hts_static_Mutex, -999);
  66.     
  67. #if HTS_WIN
  68. #else
  69.     /* Init hash */
  70.     PTHREAD_KEY_CREATE(&hts_static_key, hts_destroyvar);
  71. #endif
  72. #endif
  73.   }
  74.  
  75.   /* Set specific thread value */
  76. #if USE_BEGINTHREAD
  77. #if HTS_WIN
  78. #else
  79.   {
  80.     void* thread_val;
  81.     hts_varhash* hts_static_hash = (hts_varhash*) malloc(sizeof(hts_static_hash));
  82.     if (!hts_static_hash)
  83.       return 0;
  84.       /*
  85.       hts_static_hash->values = inthash_new(HTS_VAR_MAIN_HASH);
  86.       if (!hts_static_hash->values)
  87.       return 0;
  88.     */
  89.     hts_static_hash->blocks = inthash_new(HTS_VAR_MAIN_HASH);
  90.     if (!hts_static_hash->blocks)
  91.       return 0;
  92.     /* inthash_value_is_malloc(hts_static_hash->values, 0);  */ /* Regular values */
  93.     inthash_value_is_malloc(hts_static_hash->blocks, 1);     /* We'll have to free them upon term! */
  94.     inthash_value_set_free_handler(hts_static_hash->blocks, hts_destroyvar_key);  /* free handler */
  95.     thread_val = (void*) hts_static_hash;
  96.     
  97.     PTHREAD_KEY_SET(hts_static_key, thread_val, inthash);
  98.   }
  99. #endif
  100. #endif
  101.  
  102.   return 1;
  103. }
  104.  
  105. /*
  106.   hash table free handler to free all keys
  107. */
  108. void hts_destroyvar_key(void* adr) {
  109. #if HTS_WIN
  110. #else
  111.   hts_NostaticComplexKey* cKey = (hts_NostaticComplexKey*) adr;
  112.   if (cKey) {
  113.     void* block_address = NULL;
  114.     PTHREAD_KEY_GET(cKey->localKey, &block_address, void*);
  115.     /* Free block */
  116.     if (block_address) {
  117.       free(block_address);
  118.     }
  119.     cKey->localInit = 0;
  120.   }
  121. #endif
  122. }
  123.  
  124. void hts_destroyvar(void* ptrkey) {
  125. #if HTS_WIN
  126. #else
  127.   if (ptrkey) {
  128.     hts_varhash* hashtables = (hts_varhash*) ptrkey;
  129.     PTHREAD_KEY_SET(hts_static_key, NULL, inthash);  /* unregister */
  130.  
  131.     /* Destroy has table */
  132.     inthash_delete(&(hashtables->blocks));  /* will magically call hts_destroyvar_key(), too */
  133.     /*
  134.     inthash_delete(&(hashtables->values));
  135.     */
  136.     free(ptrkey);
  137.   }
  138. #endif
  139. }
  140.  
  141. /*
  142.   destroy all key values (for the current thread)
  143. */
  144. int hts_freevar() {
  145. #if HTS_WIN
  146. #if 0
  147.   void* thread_val = NULL;
  148.   PTHREAD_KEY_GET(hts_static_key, &thread_val, inthash);
  149.   hts_destroyvar(thread_val);
  150.   PTHREAD_KEY_SET(hts_static_key, NULL, inthash);  /* unregister */
  151.   /*
  152.   PTHREAD_KEY_DELETE(hts_static_key); NO
  153.   */
  154. #endif
  155. #endif
  156.   return 1;
  157. }
  158.  
  159. int hts_resetvar() {
  160.   int r;
  161.   hts_lockvar();
  162.   {
  163.     hts_freevar();
  164.     r = hts_initvar();
  165.   }
  166.   hts_unlockvar();
  167.   return r;
  168. }
  169.  
  170. int hts_maylockvar() {
  171.   return hts_static_Mutex_init;
  172. }
  173.  
  174. int hts_lockvar() {
  175. #if USE_BEGINTHREAD
  176.   htsSetLock(&hts_static_Mutex, 1);
  177. #endif
  178.   return 1;
  179. }
  180.  
  181. int hts_unlockvar() {
  182. #if USE_BEGINTHREAD
  183.   htsSetLock(&hts_static_Mutex, 0);
  184. #endif
  185.   return 1;
  186. }
  187.  
  188. int hts_setvar(char* name, long int value) {
  189.   return hts_setextvar(name, (long int)value, 0);
  190. }
  191.  
  192. int hts_setblkvar(char* name, void* value) {
  193.   return hts_setextvar(name, (long int)value, 1);
  194. }
  195.  
  196. int hts_setextvar(char* name, long int value, int flag) {
  197. #if HTS_WIN
  198. #else
  199.   void* thread_val = NULL;
  200.   hts_varhash* hashtables;
  201.   
  202.   /*
  203.   hts_lockvar();   // NO - MUST be protected by caller
  204.   {
  205.   */
  206.   PTHREAD_KEY_GET(hts_static_key, &thread_val, inthash);
  207.   hashtables = (hts_varhash*) thread_val;
  208.   if (hashtables) { // XXc XXC hack for win version
  209.     inthash_write(hashtables->blocks, name, value);
  210.   }
  211. #endif
  212.   
  213.   return 1;
  214. }
  215.  
  216.  
  217. int hts_getvar(char* name, long int* ptrvalue) {
  218.   return hts_getextvar(name, (long int*)ptrvalue, 0);
  219. }
  220.  
  221. int hts_getblkvar(char* name, void** ptrvalue) {
  222.   return hts_getextvar(name, (long int*)ptrvalue, 1);
  223. }
  224.  
  225. int hts_getextvar(char* name, long int* ptrvalue, int flag) {
  226. #if HTS_WIN
  227. #else
  228.   void* thread_val = NULL;
  229.   hts_varhash* hashtables;
  230.   
  231.   hts_lockvar();
  232.   {
  233.     PTHREAD_KEY_GET(hts_static_key, &thread_val, inthash);
  234.     hashtables = (hts_varhash*) thread_val;
  235.     /*  if (flag) {
  236.     */
  237.     inthash_read(hashtables->blocks, name, ptrvalue);
  238.     /*
  239.     } else {
  240.     inthash_read(hashtables->values, name, ptrvalue);
  241.     }
  242.     */
  243.   }
  244.   hts_unlockvar();
  245. #endif
  246.   
  247.   return 1;
  248. }
  249.  
  250. long int hts_directgetvar(char* name) {
  251.   long int value=0;
  252.   hts_getvar(name, &value);
  253.   return value;
  254. }
  255.  
  256. void* hts_directgetblkvar(char* name) {
  257.   void* value=NULL;
  258.   hts_getblkvar(name, &value);
  259.   return value;
  260. }
  261.